/**
 * 
 */
package org.bspv.pharma.web.bean.node.visitor;

import java.util.Map;

import org.bspv.pharma.web.bean.node.Node;
import org.bspv.pharma.web.bean.node.PlaceNode;

/**
 * @author guillaume
 *
 */
public class PlaceToHtmlVisitor extends AbstractVisitor {

	/* (non-Javadoc)
	 * @see org.bspv.pharma.webapp.bean.node.visitor.AbstractVisitor#visit(org.bspv.pharma.webapp.bean.node.Node, java.util.Map)
	 */
	@Override
	public String visit(Node node, Map<String, String> params) {
		StringBuilder sb = new StringBuilder();
		//recuperation des parametres
		String path = params.get("path");
		boolean edit = Boolean.parseBoolean(params.get("edit"));
		boolean select = Boolean.parseBoolean(params.get("select"));
		boolean standardEquipment = Boolean.parseBoolean(params.get("standardEquipment"));
		// ouverture
		sb.append("<li>");
		sb.append("<span class=\"");
		sb.append(((PlaceNode) node).getStockStatus()==null?"":((PlaceNode) node).getStockStatus().getName());
		sb.append("\">");
		sb.append(node.getName());
		sb.append("</span>");
		
		//si editable alors lien vers l'action d'edition des emplacements
		if (edit) {
			sb.append("<a href=\"");
			sb.append(path);
			sb.append("/admin/Crud/Place/update.action?element.id=");
			sb.append(node.getId());
			sb.append("\"><img src=\"");
			sb.append(path);
			sb.append("/images/common/actions/tiny_edit.png\" alt=\"\"/></a>");
		}
		
		//si selectionnable
		if (select) {
			//TODO bouton radio
		}
		
		//si standardEquipment
		if (standardEquipment) {
			sb.append("<a href=\"");
			sb.append(path);
			sb.append("/admin/Crud/StandardEquipment/list.action?placeId=");
			sb.append(node.getId());
			sb.append("\"><img src=\"");
			sb.append(path);
			/*//FIXME
			if (true Constants.STATUS_OK.equals(((PlaceNode) node).getStandardEquipmentStatus())) {
				sb.append("/images/common/tiny_ok.png\" alt=\"\"/></a>");
			} else {
				sb.append("/images/common/tiny_ko.png\" alt=\"\"/></a>");
			}
			*/
		}
		
		boolean children = false;
		if(node.getChildren() != null && node.getChildren().size()>0) {
			sb.append("<ul name=\"");
			sb.append(node.getName());
			sb.append("\"_children>");
			children = true;
		}
		for (Node child : node.getChildren()) {
			sb.append(VisitorManager.getVisitor(child.getClass()).visit(child, params));
		}
		if(children) {
			sb.append("</ul>");
		}
		// fermeture
		sb.append("</li>");
		return sb.toString();
	}

	
	
}
